package com.bonc.apicreator.ctrl;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bonc.apicreator.constant.BaseConstant;
import com.bonc.apicreator.dao.UserDao;
import com.bonc.apicreator.entity.User;
import com.bonc.apicreator.utils.Md5Util;


/**
 * 用户的controller
 *
 * @author gaopengyu
 * @version 2016年8月5日
 * @see UserController
 * @since
 */
@Controller
@RequestMapping("/user")
public class UserController {
    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    /**
     * userDao
     */
    @Autowired
    private UserDao userDao;

    /**
     * Description: 人员列表
     * 
     * @return String
     * @see
     */
    @RequestMapping(value = {""}, method = RequestMethod.GET)
    public String user() {
        return "user/list.jsp";
    }

    /**
     * Description: 获取用户
     * 
     * @param pageable
     * @param condition1
     * @return Object
     * @see
     */
    @RequestMapping(value = {"/list"})
    @ResponseBody
    public Object getUser(Pageable pageable, String condition1) {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<User> userList;
        if (StringUtils.isEmpty(condition1)) {
            userList = userDao.findAll(pageable);
        }
        else {
            userList = userDao.findByUsernameContaining(condition1, pageable);
        }
        map.put("list", userList.getContent());
        map.put("count", userList.getTotalElements());
        map.put("code", BaseConstant.STATE_SUCCESS);
        LOGGER.debug("list response:" + JSON.toJSONString(userList.getContent()));
        return JSON.toJSONString(map);

    }

    /**
     * Description: 添加普通用户
     * 
     * @param user
     * @return String
     * @see
     */
    @RequestMapping(value = {"/save"}, method = RequestMethod.POST)
    public String add(User user) {
        String password = user.getPassword();
        password = Md5Util.kl(Md5Util.md5(password));
        user.setPassword(password);
        userDao.save(user);
        return "redirect:/user";
    }

    /**
     * Description: 删除普通用户
     * 
     * @param id
     * @return String
     * @see
     */
    @RequestMapping(value = {"/delete"}, method = RequestMethod.POST)
    @ResponseBody
    public Object delete(Long id) {
        userDao.delete(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", BaseConstant.STATE_SUCCESS);
        return map;
    }

    /**
     * Description: 重复校验
     * 
     * @param username
     * @return Object
     * @see
     */
    @RequestMapping(value = {"/check"})
    @ResponseBody
    public Object check(String username) {

        User user = userDao.findByUsername(username);

        Map<String, Object> map = new HashMap<String, Object>();
        if (user != null) {
            map.put("code", BaseConstant.STATE_CHONGFU);
            map.put("message", "用户名不能重复！");
        }
        return map;
    }

    /**
     * 查询用户 Description:
     * 
     * @param model
     * @param id
     * @return Object
     * @see
     */
    @RequestMapping(value = {"/findUser"})
    @ResponseBody
    public Object findUser(Long id) {
        User user = userDao.findOne(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", BaseConstant.STATE_SUCCESS);
        map.put("message", "成功");
        map.put("user", user);
        return map;

    }

    /**
     * Description:重置密码
     * 
     * @param user
     * @return String
     * @see
     */
    @RequestMapping(value = {"/updatePassword"}, method = RequestMethod.POST)
    public String updatePassword(User user) {
        User updateUser = userDao.findOne(user.getId());
        updateUser.setPassword(Md5Util.kl(Md5Util.md5(user.getPassword())));
        userDao.save(updateUser);
        return "redirect:/user";
    }

    /**
     * Description: 修改密码
     * 
     * @param password
     * @param newPassword1
     * @param newPassword2
     * @param id
     * @param oldPassword
     * @param request
     * @return Object
     * @see
     */
    @RequestMapping(value = {"/checkPassword"})
    @ResponseBody
    public Object checkPassword(String newPassword1, String newPassword2, Long id,
                                String oldPassword, HttpServletRequest request) {

        User user = userDao.findOne(id);

        Map<String, Object> map = new HashMap<String, Object>();
        if (!user.getPassword().equals(Md5Util.kl(Md5Util.md5(oldPassword)))) {
            map.put("code", BaseConstant.STATE_CHONGFU);

            return map;

        }

        if (!newPassword1.equals(newPassword2)) {

            map.put("code", BaseConstant.STATE_FAILED);
            map.put("message", "两次密码输入的不一致");

            return map;
        }
        if (StringUtils.isEmpty(newPassword1) && StringUtils.isEmpty(newPassword2)) {
            map.put("code", 10);
            map.put("message", "新密码不可以为空");

            return map;

        }

        map.put("code", BaseConstant.STATE_SUCCESS);
        user.setPassword(Md5Util.kl(Md5Util.md5(newPassword1)));
        userDao.save(user);
        request.getSession().invalidate();
        return map;
    }

}
